package com.imy.luascript;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.content.res.Configuration;
import android.graphics.Color;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.imy.common.util.ColorUtils;
import com.imy.common.util.ALog;
import com.imy.common.util.ToastUtil;
import com.imy.lua.LuaCore;
import com.imy.common.helper.PermissionHelper;
import com.imy.lua.view.floating.FloatingManger;
import com.imy.luascript.app.ui.demo.DemoActivity;
import com.imy.touch.GestureManager;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        ALog.i("main onConfigurationChanged");
    }

    private int[] getXY(View viewById) {
        int[] location = new int[2];
        viewById.getLocationOnScreen(location);
        int width = viewById.getWidth();
        int height = viewById.getHeight();
        int i = width / 2;
        location[0] = (location[0] + i);
        location[1] = (location[1] + height / 2);
        return location;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //检测权限
        View viewById = findViewById(R.id.open_lua);
        viewById.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ToastUtil.toast(MainActivity.this,"我被长按了");
                return true;
            }
        });
        View view = findViewById(R.id.test_clicked);
        View viewById1 = findViewById(R.id.btn);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, DemoActivity.class));
                int[] xy = getXY(viewById);
                ALog.ti(" x:" + xy[0] + " y:" + xy[1]);
                int[] xy1 = getXY(viewById1);
                ALog.ti(" xy1:" + xy1[0] + " xy1:" + xy1[1]);
            }
        });
        viewById1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FloatingManger.get().start();
            }
        });
        int i = Color.parseColor("#23c268");
        String s = ColorUtils.integerToHex(i);
        ALog.i("toHexString:"+s);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
                String path =absolutePath+"/LuaAndroid/main.lua";
                LuaCore.get().start(MainActivity.this,path);
//                GestureManager.get().longClicked(540,147,0);
//                startActivity(new Intent(MainActivity.this,OpenCVTestActivity.class));
            }
        });


    }

    private void finishProcess() {
        finish();
        android.os.Process.killProcess(android.os.Process.myPid());

    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean notificationEnabled = PermissionHelper.isNotificationEnabled(this);
        if (!notificationEnabled) {
            PermissionHelper.openNotificationSetting(this);
        }
    }

    private long mBackPressTime = 0L;

    @Override
    public void onBackPressed() {
        long l = System.currentTimeMillis();
        if (l - mBackPressTime < 2000) {
            //调用系统API结束进程
            finishProcess();
        } else {
            mBackPressTime = l;
            ToastUtil.toast(LuaCore.get().getContext(), "再按一次退出应用");
        }

    }

}